package com.baijiarui.myself;

import com.fc.test.mapper.auto.*;
import com.fc.test.model.auto.*;
import com.fc.test.util.MD5Util;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;

@RestController
public class CompanyController {

    @Autowired
    ForeRecruitMapper foreRecruitMapper;

    @RequestMapping("/company/recruitList")
    public Object recruitList(){
        TsysUser temp = (TsysUser) SecurityUtils.getSubject().getPrincipal();
        String userId=temp.getId();
        ForeRecruitExample example=new ForeRecruitExample();
        example.or().andUserIdEqualTo(userId);
        return foreRecruitMapper.selectByExample(example);
    }

    @RequestMapping("/company/recruitAdd")
    public Object recruitAdd(ForeRecruit foreRecruit,String effectiveTimeStr) throws ParseException {
        TsysUser temp = (TsysUser) SecurityUtils.getSubject().getPrincipal();
        String userId=temp.getId();
        String username=temp.getUsername();
        foreRecruit.setEffectiveTime(new SimpleDateFormat("yyyy-MM-dd").parse(effectiveTimeStr));
        foreRecruit.setUserId(userId);
        foreRecruit.setName(username);
        foreRecruit.setId(UUID.randomUUID().toString());
        return foreRecruitMapper.insertSelective(foreRecruit);
    }
    @Autowired
    public ForeJobMapper foreJobMapper;
    @Autowired
    public ForeResumeMapper foreResumeMapper;

    @RequestMapping("/company/studentJobList")
    public Object studentJobList(){
        List list=foreJobMapper.selectByExample(null);
        return list;
    }

    @RequestMapping("/company/findResume")
    public Object findResume(String resumeId){
        return foreResumeMapper.selectByPrimaryKey(resumeId);
    }

    @Autowired
    ForeCompanyMapper foreCompanyMapper;

    @RequestMapping("/company/findCompany")
    public Object findCompany(){
        TsysUser temp = (TsysUser) SecurityUtils.getSubject().getPrincipal();
        String userId=temp.getId();
        ForeCompanyExample example=new ForeCompanyExample();
        example.or().andIdEqualTo(userId);
        List list=foreCompanyMapper.selectByExample(example);
        return list.size()==0?new ForeCompany():list.get(0);
    }
@Autowired
    TsysUserMapper tsysUserMapper;

    @RequestMapping("/company/saveCompany")
    public  Object saveCompany(ForeCompany foreCompany,String businessRegisterTimeStr) throws ParseException {
        //修改系统用户
        TsysUser temp = (TsysUser) SecurityUtils.getSubject().getPrincipal();
        temp.setPassword(MD5Util.encode(foreCompany.getPassword()));
        temp.setUsername(foreCompany.getUsername());
        Integer i=tsysUserMapper.updateByPrimaryKeySelective(temp);
        //修改公司信息
        foreCompany.setId(temp.getId());
        foreCompany.setBusinessRegisterTime(new SimpleDateFormat("yyyy-MM-dd").parse(businessRegisterTimeStr));
        Integer i2=foreCompanyMapper.updateByPrimaryKeySelective(foreCompany);
        return i+i2==2?"success":"fail";
    }

    @RequestMapping("/company/findRecruitById")
    public Object findRecruitById(String id){
        return foreRecruitMapper.selectByPrimaryKey(id);
    }


    @RequestMapping("/company/updateRecruit")
    public  Object updateRecruit(ForeRecruit foreRecruit,String effectiveTimeStr) throws ParseException {
        foreRecruit.setEffectiveTime(new SimpleDateFormat("yyyy-MM-dd").parse(effectiveTimeStr));
        return foreRecruitMapper.updateByPrimaryKey(foreRecruit);
    }

    @RequestMapping("/company/deleteRecruit")
    public  Object updateRecruit(String id) throws ParseException {
        return foreRecruitMapper.deleteByPrimaryKey(id);
    }






}
